package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌
 */
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 分页条件查询品牌
     */
    @GetMapping("/brand/page")
    public ResponseEntity<PageResult<Brand>> brandPageQuery(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc
    ){
        PageResult<Brand> pageResult = brandService.brandPageQuery(page,rows,key,sortBy,desc);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增品牌
     *   1）Controller使用JavaBean接收参数的情况
     *        1.1  Brand brand  接收页面的普通参数，例如：name=传智&image=xxxx.jpg&letter=C
     *        1.2 @RequestBody Brand brand  接收页面的json格式参数，例如： {name:'传智',image:'xxx.jpg',letter:'C'}
     *
     *   2）Controller如何接收同名参数？
     *       2.1 页面传递同名参数的方式
     *            1）cids=76,216,270
     *            2）cids=76&cids=216&cids=270  页面提交一个带有checkbox的form表单
     *       2.2 Controller接收同名参数的方法
     *            1）字符串接收，  String cids    76,216,270   split(",")
     *            2）数组接收     Long[] cids
     *            ***3）List集合接收  List<Long> cids   注意：如果使用List接收同名参数值，必须加上@RequestParam注解
     */
    @PostMapping("/brand")
    public ResponseEntity<Void> saveBrand(
           Brand brand,
           @RequestParam("cids") List<Long> cids
    ){
        brandService.saveBrand(brand,cids);
        //return ResponseEntity.status(HttpStatus.CREATED).body(null);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据ID查询品牌
     * @RequestParam vs @PathVariable 的用法
     *    1）@RequestParam接收接收普通参数，  例如： /user?id=10
     *    2）@PathVariable接收路径参数， 例如 /user/10
     */
    @GetMapping("/brand/{id}")
    public ResponseEntity<Brand> findBrandById(@PathVariable("id")Long id){
        Brand brand = brandService.findBrandById(id);
        return ResponseEntity.ok(brand);
    }

    /**
     * 根据分类ID查询品牌
     */
    @GetMapping("/brand/of/category")
    public ResponseEntity<List<Brand>> findBrandsByCid(@RequestParam("id")Long id){
        List<Brand> brands = brandService.findBrandsByCid(id);
        return ResponseEntity.ok(brands);
    }

    /**
     * 根据id查询批量查询品牌
     */
    @GetMapping("/brand/list")
    public ResponseEntity<List<Brand>> findBrandsByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> brands = brandService.findBrandsByIds(ids);
        return ResponseEntity.ok(brands);
    }
}
